# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


# class ScriptTestPipeline(object):
#     def process_item(self, item, spider):
#         return item
import json
import pymysql

#负责写入文件的管道类
class JsonPipeline(object):
    def __init__(self):
        self.filename='default.json'
    def open_spider(self,spider):
        print("开始执行下载")
        #打开文件
        self.f = open(self.filename,"w",encoding="utf-8")
    def process_item(self,item,spider):
        #执行写入
        self.f.write(json.dumps(dict(item),ensure_ascii = False )+ "\n")
        return item
    def close_spider(self,spider):
        #关闭文件
        self.f.close()
        print("结束下载")


class XdlPipeline(JsonPipeline):
    def __init__(self):
        self.filename="xdl.json"

class CarPipeline(JsonPipeline):
    def __init__(self):
        self.filename = "car.json"
from scrapy.pipelines.images import ImagesPipeline

class ImgPipeline(ImagesPipeline):
    def item_completed(self, results, item, info):
        img_path = []
        for res in results:
           if (res[0]):
                path = res[1]["path"]
                img_path.append(path)
        img_path=','.join(img_path)

        item["img_path"] = img_path
        return item

class YuehuiPipeline(JsonPipeline):
    def __init__(self):
        self.filename="yuehui.json"

#执行sql写入
class MysqlPipeline(object):
    def open_spider(self,spider):
        #打开数据库链接
        self.db =pymysql.connect("127.0.0.1","root","123456","neihan",charset="utf8")
        self.cursor = self.db.cursor()
        print("执行写入")
    def process_item(self,item,spider):
        #执行写入
        sql,data = item.get_sql()
        print(sql,data)
        try:
            self.cursor.execute(sql,data)
            self.db.commit()
        except Exception as e:
            print(e)
            self.db.rollback()
        return item
    def close_spider(self,spider):
        #关闭数据库
        self.cursor.close()
        self.db.close()
        print("写入完毕")

class WangzheImgPipeline(ImagesPipeline):
    def item_completed(self, results, item, info):
        skin_url= []
        for res in results:
            if res[0]:
                skin_url.append(res[1]["path"])
        item["skin_url"]=str(skin_url)
        return item

class WangzheMysqlPipeline(object):
    def open_spider(self,spider):
        self.db = pymysql.connect("127.0.0.1","root","123456","neihan",charset="utf8")
        self.cursor = self.db.cursor()
    def process_item(self,item,spider):
        sql,data = (item.get_sql())

        try:
            self.cursor.execute(sql,data)
            self.db.commit()
        except Exception as e:
            print(e)
            self.db.rollback()
        return item

    def close_spider(self,spider):
        self.cursor.close()
        self.db.close()

class CnblogsPipeline(object):
    def open_spider(self,spider):
        self.db = pymysql.connect('127.0.0.1',"root",'123456','neihan',charset="utf8")
        self.cursor = self.db.cursor()
    def process_item(self,item,spider):
        item["spider"] = spider.name
        sql,data = item.get_sql()
        try:
            self.cursor.execute(sql,data)
            self.db.commit()
        except Exception as e:
            print(item["info_url"],e)
            self.db.rollback()
        return item
    def close_spider(self,spider):
        self.cursor.close()
        self.db.close()
